(function(){

    // 全局方法定义
    window.loadPlug = {
        loadJs: function (props,callback) {
            if(!props) return false;
            for(var i = 0; i < props.length; i++ ){
                this._readyState(this._urlScript(props[i]),callback);
            }
        },
        loadCss: function(props){
            if(!props) return false;
            for(var i = 0; i < props.length; i++ ){
                this._urlLink(props[i]);
            }
        },
        _urlScript: function(url){
            this._script = document.createElement("script");
            this._script.setAttribute('type','text/javascript');
            this._script.setAttribute('src',layui.setter.base + url+'.js');
            document.getElementsByTagName('head')[0].appendChild(this._script);
            return this._script;
        },
        _urlLink: function(url){
            this._link = document.createElement('link');
            this._link.setAttribute('rel','stylesheet');
            this._link.setAttribute('type','text/css');
            this._link.setAttribute('href',layui.setter.base + url+'.css');
            document.getElementsByTagName('head')[0].appendChild(this._link);
            return this._link;
        },
        _readyState: function(_script,callback){
            if(_script.readyState){
                _script.onreadystatechange=function(){
                    if(_script.readyState=='complete'||_script.readyState=='loaded'){
                        _script.onreadystatechange=null;
                        typeof callback == "function" ? callback() : null;
                    }
                };
            }else{
                _script.onload=function(){
                    typeof callback == "function" ? callback() : null;
                };
            }
        }
    };


    window.observerObj = function(props){
        if (props.name && props.name.length > 0 ) {
            for(var i = 0; i < props.name.length ; i++){
                var name = props.name[i];
                var config = {
                    attributes: true
                };
                var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
                var observer = new MutationObserver(function (mutations) {
                    mutations.forEach(function (mutation) {
                        typeof props.callback == "function" ? props.callback(mutation) : null;
                        observer.disconnect();
                        observer.observe(name, config);
                    });
                });
                observer.observe(name, config);
            }
        }
    };

    // 构建节点后添加方法
    window.insertAfter = function (newElement, targetElement) {
        var parent = targetElement.parentNode;
        if(parent.lastChild == targetElement) {
            parent.appendChild(newElement);
        }else{
            parent.insertBefore(newElement, targetElement.nextSibling);
        }
    };

    window.ice = {
        loadJs:function(path,callback){
            loadPlug.loadJs(path,callback);
        },
        loadCss:function(path){
            loadPlug.loadCss(path);
        },
        ie8:function(callback){
            if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE8.0"){
                setTimeout(function(){
                    typeof callback == "function" ? callback() : null;
                },0);
            }
        }
    };
    //  节流
    var timeOut;
    window.timers = function (callback,time){
        clearTimeout(timeOut);
        timeOut = setTimeout(function(){
            typeof callback == "function" ? callback() : null;
        }, (time || 500));
    };

    //获取url参数值
    window.getQueryParam = function(name) {
        var url = window.location.href;
        var reg = new RegExp('[?&]' + name + '=([^&#]+)');
        var query = url.match(reg);
        return query ? query[1] : null;
    }



    // rgb转十六进制色值
    String.prototype.colorHex = function(){
        var that = this;
        if(/^(rgb|RGB)/.test(that)){
            var aColor = that.replace(/(?:\(|\)|rgb|RGB)*/g,"").split(",");
            var strHex = "#";
            for(var i=0; i<aColor.length; i++){
                var hex = Number(aColor[i]).toString(16);
                if(hex === "0"){
                    hex += hex; 
                }
                if(hex.length <= 1){
                    hex = '0'+hex;
                }
                strHex += hex;
            }
            if(strHex.length !== 7){
                strHex = that;  
            }
            return strHex;
        }else if(/^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/.test(that)){
            var aNum = that.replace(/#/,"").split("");
            if(aNum.length === 6){
                return that;    
            }else if(aNum.length === 3){
                var numHex = "#";
                for(var i=0; i<aNum.length; i+=1){
                    numHex += (aNum[i]+aNum[i]);
                }
                return numHex;
            }
        }else{
            return that;    
        }


    };

}((function(){
    window.HTMLElement = window.HTMLElement || Element;
   // ie assign 方法重构
   if (typeof Object.assign != 'function') {
        Object.assign = function(target) {
        if (target == null) {
            throw new TypeError('异常');
        }
        target = Object(target);
        for (var index = 1; index < arguments.length; index++) {
            var source = arguments[index];
            if (source != null) {
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key];
                }
            }
            }
        }
        return target;
        };
    }

    if ( !Array.prototype.forEach ) {
        Array.prototype.forEach = function forEach( callback, thisArg ) {
            var T, k;
            if ( this == null ) {
                throw new TypeError( "this is null or not defined" );
            }
            var O = Object(this);
            var len = O.length >>> 0;
            if ( typeof callback !== "function" ) {
                throw new TypeError( callback + " is not a function" );
            }
            if ( arguments.length > 1 ) {
                T = thisArg;
            }
            k = 0;
            while( k < len ) {
     
                var kValue;
                if ( k in O ) {
                    kValue = O[ k ];
                    callback.call( T, kValue, k, O );
                }
                k++;
            }
        };
    }


    if (!Array.prototype.indexOf){
        Array.prototype.indexOf = function(elt /*, from*/){
          var len = this.length >>> 0;
      
          var from = Number(arguments[1]) || 0;
          from = (from < 0)
               ? Math.ceil(from)
               : Math.floor(from);
          if (from < 0)
            from += len;
      
          for (; from < len; from++){
            if (from in this && this[from] === elt)
              return from;
          }
          return -1;
        };
    }




    if (!("classList" in document.documentElement)) {
        Object.defineProperty(HTMLElement.prototype, 'classList', {
            get: function() {
                var self = this;
                function update(fn) {
                    return function(value) {
                        var classes = self.className.split(/\s+/g),
                            index = classes.indexOf(value);
     
                        fn(classes, index, value);
                        self.className = classes.join(" ");
                    };
                }
     
                return {
                    add: update(function(classes, index, value) {
                        if (!~index) classes.push(value);
                    }),
     
                    remove: update(function(classes, index) {
                        if (~index) classes.splice(index, 1);
                    }),
     
                    toggle: update(function(classes, index, value) {
                        if (~index)
                            classes.splice(index, 1);
                        else
                            classes.push(value);
                    }),
     
                    contains: function(value) {
                        return !!~self.className.split(/\s+/g).indexOf(value);
                    },
     
                    item: function(i) {
                        return self.className.split(/\s+/g)[i] || null;
                    }
                };
            }
        });
    }




})()));

layui.define(['setter'], function(exports){
    exports('ice', ice);
});





